package com.b2c.web;

import com.b2c.common.enums.ExceptionEnums;
import com.b2c.common.exception.MrException;
import com.b2c.service.CategoryService;
import com.mr.service.pojo.CategoryEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import java.util.Map;

@RestController
@RequestMapping("category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;
    @GetMapping("list")
    public ResponseEntity<List<CategoryEntity>> getCateList(
            @RequestParam(value = "pid",defaultValue = "0") Long pid){
        List<CategoryEntity> list=categoryService.queryCategoryByPid(pid);
        if(list == null && list.size()==0){
            throw new MrException(ExceptionEnums.CATEGORY_CANNOT_BE_NULL);
        }
        //调用service
        return ResponseEntity.ok(list);
    }

    @PostMapping("post")
    public void save(@RequestBody CategoryEntity entity){
        categoryService.save(entity);

    }

    //删除
    @DeleteMapping("delete")
    public  ResponseEntity delCate(@RequestParam(value = "id") Long id){
        try {
            categoryService.delCategoryById(id);
        }catch (MrException mr){
            //删除错误
            throw new MrException(ExceptionEnums.DEL_CATEGORY_ERROR);
        }

        return ResponseEntity.ok("删除成功");
    }

    //查询品牌下的分类
    @GetMapping("queryCategoryByBrandId/{bid}")
    public ResponseEntity<List<CategoryEntity>> queryCategoryByBrandId(@PathVariable("bid") Long bid){

        List<CategoryEntity> list= categoryService.queryCategoryListByBid(bid);
        if (list.size()==0){

        }
        return ResponseEntity.ok(list);
    }

    @PutMapping("put")
    public void update(@RequestBody CategoryEntity entity){
        categoryService.update(entity);
    }

    //根据分类ids 查询多个分类
    @GetMapping("queryCateLisByIds")
    public ResponseEntity<List<CategoryEntity>> queryCateLisByIds(@RequestParam("ids") List ids){
        return ResponseEntity.ok(categoryService.queryCategoryByids(ids));
    }
    @GetMapping("cateNames")
    public  ResponseEntity<List<String>> queryCateNameByIds(@RequestParam("ids") List<Long> ids){
        List<String> nameList= categoryService.queryCateNameByIds(ids);
        return ResponseEntity.ok(nameList);
    }


    /**
     * 前台页面分类展示
     * @return
     */
    @GetMapping("queryCate")
    public ResponseEntity<Map<String, Map<String,List<String>>>> queryCate(){

        Map<String,Map<String,List<String>>> map = categoryService.queryCate();
        return ResponseEntity.ok(map);
    }

}
